package com.studyv3.test.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyv3.common.core.domain.AjaxResult;
import com.studyv3.common.core.domain.entity.SysUser;
import com.studyv3.common.utils.PageUtils;
import com.studyv3.system.mapper.SysUserMapper;
import com.studyv3.test.domain.ApplyProduct;
import com.studyv3.test.domain.Batch;
import com.studyv3.test.domain.Products;
import com.studyv3.test.mapper.ApplyProductMapper;
import com.studyv3.test.mapper.BatchMapper;
import com.studyv3.test.mapper.ProductsMapper;
import com.studyv3.test.service.ApplyProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * lb_购买申请/审批 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-07
 */
@Service
public class ApplyProductServiceImpl extends ServiceImpl<ApplyProductMapper, ApplyProduct> implements ApplyProductService {

    @Autowired
    ApplyProductMapper applyProductMapper;
    @Autowired
    BatchMapper batchMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    ProductsMapper productsMapper;

    @Override
    public List<ApplyProduct> selectApplyProductList(ApplyProduct applyProduct) {

        List<ApplyProduct>list= applyProductMapper.selectList(Wrappers.<ApplyProduct>lambdaQuery()
                .eq(applyProduct.getFlag()!=null,ApplyProduct::getFlag,applyProduct.getFlag())
                .eq(ApplyProduct::getDeleted,applyProduct.getDeleted())
                .eq(applyProduct.getWarehousFlag()!=null,ApplyProduct::getWarehousFlag,applyProduct.getWarehousFlag())
                .eq(applyProduct.getBatchId()!=null,ApplyProduct::getBatchId,applyProduct.getBatchId())
                        .orderByDesc(ApplyProduct::getFlag)
                        .orderByDesc(ApplyProduct::getCreateTime)
                );
        if(!list.isEmpty()){
            List<Integer> applicants = list.stream().map(ApplyProduct::getApplicant).collect(Collectors.toList());
            List<Integer> batchIds = list.stream().map(ApplyProduct::getBatchId).collect(Collectors.toList());
            List<Integer> productIds = list.stream().map(ApplyProduct::getProductsId).collect(Collectors.toList());
            Map<Long,String> a =sysUserMapper.selectList(Wrappers.<SysUser>query()
                    .select("user_id","user_name")
                            .in("user_id",applicants)).stream().collect(Collectors.toMap(SysUser::getUserId,SysUser::getUserName));
            Map<Integer,String> b =batchMapper.selectList(Wrappers.<Batch>query()
                    .select("id","name")
                    .in("id",batchIds)).stream().collect(Collectors.toMap(Batch::getId,Batch::getName));
            Map<Integer,String> p =productsMapper.selectList(Wrappers.<Products>query()
                    .select("id","name")
                    .in("id",productIds)).stream().collect(Collectors.toMap(Products::getId,Products::getName));
            for (ApplyProduct applyProduct1 : list) {
                applyProduct1.setProductsName(p.get(applyProduct1.getProductsId()));
                applyProduct1.setApplicantName(a.get(applyProduct1.getApplicant().longValue()));
                applyProduct1.setBatchName(b.get(applyProduct1.getBatchId()));
            }
        }

        return list;
    }

    @Override
    public List<ApplyProduct> selectMyApplyProductList(ApplyProduct applyProduct) {
        PageUtils.startPage();
        List<ApplyProduct>list= applyProductMapper.selectList(Wrappers.<ApplyProduct>lambdaQuery()
                        .eq(ApplyProduct::getDeleted,0)
                .eq(ApplyProduct::getApplicant,applyProduct.getApplicant())
                .le(applyProduct.getEndTime()!=null,ApplyProduct::getCreateTime,applyProduct.getEndTime())
                .ge(applyProduct.getStartTime()!=null,ApplyProduct::getCreateTime,applyProduct.getStartTime())
                .eq(applyProduct.getFlag()!=null,ApplyProduct::getFlag,applyProduct.getFlag())
                        .orderByDesc(ApplyProduct::getFlag)
                        .orderByDesc(ApplyProduct::getCreateTime)
                );
        if(!list.isEmpty()){
            List<Integer> batchIds = list.stream().map(ApplyProduct::getBatchId).collect(Collectors.toList());
            List<Integer> productIds = list.stream().map(ApplyProduct::getProductsId).collect(Collectors.toList());
            Map<Integer,String> b =batchMapper.selectList(Wrappers.<Batch>query()
                    .select("id","name")
                    .in("id",batchIds)).stream().collect(Collectors.toMap(Batch::getId,Batch::getName));
            Map<Integer,String> p =productsMapper.selectList(Wrappers.<Products>query()
                    .select("id","name")
                    .in("id",productIds)).stream().collect(Collectors.toMap(Products::getId,Products::getName));
            for (ApplyProduct applyProduct1 : list) {
                applyProduct1.setApplicantName(applyProduct.getApplicantName());
                applyProduct1.setProductsName(p.get(applyProduct1.getProductsId()));
                applyProduct1.setBatchName(b.get(applyProduct1.getBatchId()));
            }
        }

        return list;
    }

/*
我的已审
        */
    public List<ApplyProduct> selectApplyByMeProductList(ApplyProduct applyProduct) {
        PageUtils.startPage();
        List<ApplyProduct>list= applyProductMapper.selectList(Wrappers.<ApplyProduct>lambdaQuery()
                .ne(ApplyProduct::getFlag,applyProduct.getFlag())
                .eq(ApplyProduct::getDeleted,applyProduct.getDeleted())
                .eq(ApplyProduct::getApprovalUser,applyProduct.getApprovalUser())
                .orderByDesc(ApplyProduct::getCreateTime));
        if(!list.isEmpty()){
            List<Integer> applicants = list.stream().map(ApplyProduct::getApplicant).collect(Collectors.toList());
            List<Integer> batchIds = list.stream().map(ApplyProduct::getBatchId).collect(Collectors.toList());
            List<Integer> productIds = list.stream().map(ApplyProduct::getId).collect(Collectors.toList());
            Map<Long,String> a =sysUserMapper.selectList(Wrappers.<SysUser>query()
                    .select("user_id","user_name")
                    .in("user_id",applicants)).stream().collect(Collectors.toMap(SysUser::getUserId,SysUser::getUserName));
            Map<Integer,String> b =batchMapper.selectList(Wrappers.<Batch>query()
                    .select("id","name")
                    .in("id",batchIds)).stream().collect(Collectors.toMap(Batch::getId,Batch::getName));
            Map<Integer,String> p =productsMapper.selectList(Wrappers.<Products>query()
                    .select("id","name")
                    .in("id",productIds)).stream().collect(Collectors.toMap(Products::getId,Products::getName));
            for (ApplyProduct applyProduct1 : list) {
                applyProduct1.setProductsName(p.get(applyProduct1.getProductsId()));
                applyProduct1.setApplicantName(a.get(applyProduct1.getApplicant().longValue()));
                applyProduct1.setBatchName(b.get(applyProduct1.getBatchId()));
            }
        }

        return list;
    }

    @Override
    public ApplyProduct selectApplyProductById(Integer id) {
        return applyProductMapper.selectById(id);
    }


    @Override
    public AjaxResult addApplyProduct(ApplyProduct applyProduct) {
        applyProduct.setDeleted(0);
        applyProduct.setCreateTime(LocalDateTime.now());
        applyProduct.setFlag(2);
        Products products = productsMapper.selectById(applyProduct.getProductsId());
        applyProduct.setPrice(products.getPrice().multiply(new BigDecimal(applyProduct.getAmount())));
        Batch batch = batchMapper.selectId(applyProduct.getBatchId());
        if(batch.getBalance().compareTo(applyProduct.getPrice())<0){
            return AjaxResult.error("预算不足");
        }
        int i = applyProductMapper.insert(applyProduct);
        if (i > 0){
            return AjaxResult.success("提交成功");
        }
        return AjaxResult.error("提交失败");
    }

    @Override
    public AjaxResult updateApplyProduct(ApplyProduct applyProduct) {
        applyProduct.setUpdateTime(LocalDateTime.now());
        Products products = productsMapper.selectById(applyProduct.getProductsId());
        applyProduct.setPrice(products.getPrice().multiply(new BigDecimal(applyProduct.getAmount())));
        if(batchMapper.selectId(applyProduct.getBatchId()).getBalance().compareTo(applyProduct.getPrice())<0){
            return AjaxResult.error("预算不足");
        }
        int i = applyProductMapper.updateById(applyProduct);
        if (i > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @Override
    public AjaxResult warehousApplyProduct(ApplyProduct applyProduct) {
        applyProduct.setUpdateTime(LocalDateTime.now());
        Products products = productsMapper.selectById(applyProduct.getProductsId());
        applyProduct.setPrice(products.getPrice().multiply(new BigDecimal(applyProduct.getAmount())));
        int i = applyProductMapper.updateById(applyProduct);
        if (i > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @Override
    public AjaxResult deleteApplyProduct(Integer[] ids) {
        ApplyProduct applyProduct = new ApplyProduct();
        int i=0;
        for(Integer id : ids){
            applyProduct.setId(id);
            applyProduct.setDeleted(1);
            i=i+applyProductMapper.updateById(applyProduct);
//            i=i+applyProductMapper.deleteById(applyProduct);
        }


        return AjaxResult.success("删除了"+i+"条数据");

    }




}


